﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SquidConsult.BLL;
using System.IO;

namespace SquidConsult.UI.Accountant
{
    public partial class _default : System.Web.UI.Page
    {
        #region Loading page

        /// <summary>
        /// Load Accoutant Page
        /// </summary>
        private AccountantBL accountant = new AccountantBL();

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadReport();
            LoadBudget();
            LoadSupervisorName();
        }

        private void LoadReport()
        {
            try
            {
                ReportGridView.DataSource = accountant.GetAllReport();
                ReportGridView.DataBind();
            }
            catch (Exception ex)
            {
                LoadErrorLabel.Text = "Report load fail : The following error occured: " + ex.Message;
            }
        }

        private void LoadBudget()
        {
            try
            {
                ExpenseLabel.Text = accountant.GetCompanyTotalExpense().ToString();
                BugetLabel.Text = accountant.GetCompanyRemainBudget().ToString();
            }
            catch (Exception ex)
            {
                LoadErrorLabel.Text = "Budget load fail : The following error occured: " + ex.Message;
            }
        }

        private void LoadSupervisorName()
        {
            if(!IsPostBack)
            {
                SupervisorNameDropDownList.DataSource = accountant.GetSupervisorName();
                SupervisorNameDropDownList.DataBind();
            }
        }

        protected void SupervisorNameDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupervisorTotalExpenseLabel.Text = accountant.GetSupervisorTotalExpense(SupervisorNameDropDownList.SelectedValue.ToString()).ToString();
        }
        #endregion 

        #region See Expense Reports

        /// <summary>
        /// Sell all expense reports that have been submitted by 
        /// consutltant and approved by department supervisor
        /// </summary> 
        protected void ReportGridView_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //make reportId and expenseId invisible 
            e.Row.Cells[1].CssClass = "hiddencol";
        }

        protected void ReportGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ExpenseGridView.DataSource = accountant.GetExpense(Guid.Parse(ReportGridView.SelectedRow.Cells[1].Text));
                ExpenseGridView.DataBind();
                UpdateVisible();
            }
            catch (Exception ex)
            {
                LoadErrorLabel.Text = "Expense load fail : The following error occured: " + ex.Message;
            }
        }

        private void UpdateVisible()
        {
            ChangeStatusPanel.Visible = true;
            StatusDropDownList.SelectedValue = ReportGridView.SelectedRow.Cells[6].Text;
        }
    
        protected void ExpenseGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            DownloadPanel.Visible = true;
        }
        #endregion 

        #region Approve or Reject report
        /// <summary>
        /// Approve or Reject report
        /// Hightlight Expense will result in a department ina department going over hight
        /// </summary>
        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            try
            {
                accountant.UpdateStatus(Guid.Parse(ReportGridView.SelectedRow.Cells[1].Text), StatusDropDownList.SelectedValue.ToString());
                LoadReport();
                HighlightReport();
                LoadBudget();
            }
            catch (Exception ex)
            {
                LoadErrorLabel.Text = "Report update fail : The following error occured: " + ex.Message;
            }
        }

        //Hightlight Expense will result in a department in a department going over budget
        private void HighlightReport()
        {
            // if accountant  approving a report and the result goning over budget
            if (StatusDropDownList.SelectedIndex == 1 && accountant.GetRemainBuget(ReportGridView.SelectedRow.Cells[5].Text) < 0)
                ReportGridView.SelectedRow.ForeColor = System.Drawing.Color.Red;
            else
                ReportGridView.SelectedRow.ForeColor = System.Drawing.Color.Black;
        }

        #endregion 

        #region Download Receipt
        /// <summary>
        /// Download Each expense receipt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DownloadButton_Click(object sender, EventArgs e)
        {
            try
            {
                string receiptPath = accountant.GetReceiptPath(Guid.Parse(ExpenseGridView.SelectedRow.Cells[1].Text));
                FileStream fileStream = new FileStream(receiptPath, FileMode.Open, FileAccess.Read);
                Byte[] binaryData = new Byte[fileStream.Length];
                fileStream.Read(binaryData, 0, (Int32)fileStream.Length);
                fileStream.Close();
                Response.ContentType = "pdf";
                Response.AddHeader("Content-Disposition", "attachment; filename=Receipt.pdf");
                Response.AddHeader("Content-length", binaryData.Length.ToString());
                Response.BinaryWrite(binaryData);
                Response.Flush();
                Response.Clear();
            }
            catch (System.IO.FileNotFoundException)
            {
                ErrorLabel.Text = "The receipt does not exist";
            }
        }
        #endregion 
    }
}